﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace 第四次作业
{
    public delegate void AlarmEventHandler(DateTime dateTime);
    
    public class AlarmClock
    {
        public event AlarmEventHandler TickEvent;
        public event AlarmEventHandler AlarmEvent;

        public void Working()
        {
            while(true)
            {
                System.Threading.Thread.Sleep(1000);
                if(TickEvent!=null)
                {
                    TickEvent(DateTime.Now);
                }
                if (AlarmEvent != null)
                {
                    if(DateTime.Now.Second%3 == 00)
                    AlarmEvent(DateTime.Now);
                }
            }
        }
    }

    public class DoIt
    {
        static void Main()
        {
            AlarmClock  alarm = new AlarmClock();
            alarm.TickEvent += Ticking;
            alarm.AlarmEvent += Alarming;
            alarm.Working();
        }
        static void Ticking(DateTime dateTime)
        {
            Console.WriteLine("Now:{0}", dateTime);
        }
        static void Alarming(DateTime dateTime)
        {
            Console.WriteLine("ALARM!!!!!!!!!!!!!!!!!!!!!!!");
        }
    }






















































    /*public class DownloadEventArgs : EventArgs
    {
        public double Percent;
    }
    public delegate void DownloadEventHandler(object sender, DownloadEventArgs e);

    public class Downloader
    {
        public event DownloadEventHandler Downing;

        public void DoDownload()
        {
            double total = 10000;
            double already = 0;
            Random random = new Random();
            while (already < total)
            {
                System.Threading.Thread.Sleep(500);
                already += (random.NextDouble() / 4) * total;
                if (already > total) already = total;
                if (Downing != null)
                {
                    DownloadEventArgs args = new DownloadEventArgs();
                    args.Percent = already / total;
                    Downing(this, args);
                }
            }
        }
    }
    class UserDownloader
    {
        static void Main(string[] args)
        {
            var downloader = new Downloader();
            downloader.Downing += ShowProgress;
            downloader.DoDownload();
        }
        static void ShowProgress(object sender, DownloadEventArgs e)
        {
            Console.WriteLine($"Downloading...{e.Percent:##.#%}");
        }
    }*/
}
